#define _CRT_SECURE_NO_WARNINGS 1

#include"DList.h"

ListNode* creadNode(LTDataType x)
{
	ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
	if (temp == NULL)
	{
		perror("malloc fail !\n");
		return -1;
	}
	temp->data = x;
	temp->next = NULL;
	temp->prev = NULL;
	return  temp;

}

// 创建返回链表的头结点.
ListNode* ListCreate()
{
	ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
	if (temp == NULL)
	{
		perror("malloc fail !\n");
		return -1;
	}
	temp->next = temp;
	temp->prev = temp;
	temp->data = 0;
	return  temp;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	//头节点一定不会为空
	assert(pHead);
	if (pHead->next == pHead)
	{
		free(pHead);
		pHead = NULL;
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	printf("销毁成功！");
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is NULL!\n");
		exit(-1);
	}
	printf("哨兵位<=>");
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	ListNode* newNode = creadNode(x);
	ListNode* tail = pHead->next;
	while (tail->next != pHead)
	{
		tail = tail->next;
	}
	newNode->next = pHead;
	pHead->prev = newNode;
	tail->next = newNode;
	newNode->prev = tail;
}

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is NULL!\n");
		exit(-1);
	}
	ListNode* tail = pHead->prev;
	ListNode* prev = tail->prev;
	pHead->prev = prev;
	prev->next = pHead;
	free(tail);

}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	ListNode* newNode = creadNode(x);
	newNode->next = pHead->next;
	pHead->prev = newNode;
	pHead->next = newNode;
	newNode->prev = pHead;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	if (pHead->next == pHead)
	{
		printf("List is  NULL!\n");
		exit(-1);
	}
	ListNode* firstNode = pHead->next;
	ListNode* secondNode = firstNode->next;
	pHead->next = secondNode;
	secondNode->prev = pHead;
	free(firstNode);
	firstNode = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pHead, ListNode* pos, LTDataType x)
{
	assert(pos);
	if (pos == pHead)
	{
		ListPushBack(pHead, x);
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == pos->data)
		{
			ListNode* prev = pos->prev;
			ListNode* newNode = creadNode(x);
			prev->next = newNode;
			newNode->prev = prev;
			newNode->next = pos;
			pos->prev = newNode;
			return;
		}
		cur = cur->next;
	}
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pHead, ListNode* pos)
{
	assert(pHead);
	if (pos == pHead->next)
	{
		ListPopFront(pHead);
	}
	if (pos == pHead->prev)
	{
		ListPopBack(pHead);
	}
	ListNode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->data == pos->data)
		{
			ListNode* prev = cur->prev;
			ListNode* next = cur->next;
			prev->next = next;
			next->prev = prev;
			free(cur);
			return;
		}
		cur = cur->next;
	}
}